package com.dg11185.nearshop.activity.home;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.android.nearshop.R;

public class WebviewActivity extends Activity {
    /** Called when the activity is first created. */
    private WebView mainWebView;

    private ProgressBar webBar;
    
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.web_activity);

        webBar = (ProgressBar) this.findViewById(R.id.view_download_progress);
        mainWebView = (WebView) this.findViewById(R.id.activity_webview);
        mainWebView.setWebChromeClient(new MyWebChromeClient());
        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        String url = getIntent().getStringExtra("webviewUrl");
        mainWebView.loadUrl(url);
        // mainWebView.loadUrl("http://www.baidu.com/");
    }

    final class MyWebChromeClient extends WebChromeClient {
        @Override
        public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
            System.out.println(message);
            result.confirm();
            return true;
        }

        @Override
        public void onProgressChanged(WebView view, int progress) {
            super.onProgressChanged(view, progress);
            webBar.setProgress(progress);
        }
    }

    class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }
    }

    // 不设置该内部类点击链接会跳转到浏览器
    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    // 设置回退 否者返回操作会直接退出程序
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (mainWebView.canGoBack()) {
                mainWebView.goBack(); // goBack()表示返回WebView的上一页面
                return true;
            } else {
                this.finish();
            }
        }
        return false;
    }
}
